package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.controller.po.InsertClazzParam;
import com.itheima.tlias84.controller.po.SelectClazzPageParam;
import com.itheima.tlias84.dao.entity.Clazz;
import com.itheima.tlias84.dao.mapper.ClazzMapper;
import com.itheima.tlias84.service.ClazzService;
import com.itheima.tlias84.service.dto.SelectClazzDTO;
import com.itheima.tlias84.service.vo.PageVO;
import com.itheima.tlias84.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    ClazzMapper clazzMapper;

    @Override
    public List<Clazz> selectAll() {
        return clazzMapper.selectAll();
    }

    @Override
    public ResultUtil selectPage(SelectClazzPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectClazzDTO> list = clazzMapper.selectPage(param);
        list.forEach(c -> {
            if (c.getBeginDate().isAfter(LocalDate.now())) {
                c.setStatus("未开班");
            } else if (c.getEndDate().isBefore(LocalDate.now())) {
                c.setStatus("已结课");
            } else {
                c.setStatus("已开班");
            }
        });

        PageVO<Object> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(list);
        return ResultUtil.success(pageVO);
    }

    @Override
    public SelectClazzDTO selectById(Integer id) {
        return clazzMapper.selectById(id);
    }

    @Override
    public int insertClazz(InsertClazzParam param) {
        return clazzMapper.insertClazz(param);
    }

    @Override
    public int deleteClazz(Integer id) {
        return clazzMapper.deleteClazz(id);
    }

    @Override
    public int updateClazzById(InsertClazzParam param) {
        String s = clazzMapper.selectByName(param);
        if (s != null) {
            return -1;
        }else {
            return clazzMapper.updateClazzById(param);
        }
    }


}
